﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using TestWebMsgApp;

public partial class WebUserControl_ControlChuyenDoanhSoGiuaCacBA : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    static string error = "";
    static bool flag = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                GetTTPP();
                txtNgayHoaDon.Text = DateTime.Now.ToString("dd/MM/yyyy");
            }
            catch (Exception ex)
            {
                BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
                string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
                obj.ErrorLog(ex.Message, method);
            }
        }
    }
    protected void btCheck_Click(object sender, EventArgs e)
    {
        try
        {
            dt = new ClassExecute.ClassExecute();
            int iCheckMonth = 1;
            iCheckMonth = dt.ExecuteSQl_int(str, "[ls_isClosedMonth]", CommandType.StoredProcedure, ref error,
                new SqlParameter("@selDate", txtNgayHoaDon.Text.Trim()),
                new SqlParameter("@siteCode", DropTTPP.SelectedValue.ToString()));
            if (iCheckMonth == 0)
            {

                string NameBADuocChuyen = "Not Existed";
                NameBADuocChuyen = dt.GetDataDepend(str, "[ls_getBAName]", CommandType.StoredProcedure, ref error, new SqlParameter("@saleSpKey", txtMaBA2.Text.Trim()));
                if (string.Equals(NameBADuocChuyen, "Not Existed"))
                {
                    uscMsgBox1.AddMessage("Mã BA được chuyển " + txtMaBA2.Text + "không tồn tại trong hệ thống,xin nhập lại", uscMsgBox.enmMessageType.Error);
                   
                    txtMaBA2.Focus();
                }
                else
                {
                    txtHoTenBA2.Text = NameBADuocChuyen;
                    int iCheckHD = dt.ExecuteSQl_int(str, "[ls_isValidInvoce4Change]", CommandType.StoredProcedure, ref error,
                        new SqlParameter("@siteCode", DropTTPP.SelectedValue.ToString()),
                        new SqlParameter("@invDate", txtNgayHoaDon.Text.Trim()),
                        new SqlParameter("@salespKey", txtMaBA.Text.Trim()),
                        new SqlParameter("@invNo", txtSoHoaDon.Text.Trim()));
                    if (iCheckHD == 0)
                    {
                        txtMaBA.Text = "";
                        txtHoTenBA.Text = "";
                        txtSoHoaDon.Text = "";
                        txtTienHD.Text = "";
                        uscMsgBox1.AddMessage("Không có đơn hàng số " + txtSoHoaDon.Text + " của BA " + txtMaBA.Text + " thuộc TTPP " + DropTTPP.SelectedItem.ToString() + " phát sinh ngày " + txtNgayHoaDon.Text, uscMsgBox.enmMessageType.Error);

                    }
                    else if (iCheckHD == 1)
                    {
                        txtHoTenBA.Text = dt.GetDataDepend(str, "[ls_getBAName]", CommandType.StoredProcedure, ref error, new SqlParameter("@saleSpKey", txtMaBA.Text.Trim()));
                        txtTienHD.Text = dt.GetDataDepend(str, "[ls_getInvoiceAmount]", CommandType.StoredProcedure, ref error, new SqlParameter("@invDate", txtNgayHoaDon.Text.Trim()), new SqlParameter("@invNo", txtSoHoaDon.Text));
                        LockText();
                        flag = true;
                    }

                }
            }
            else
            {
                
                uscMsgBox1.AddMessage("Ngày " + txtNgayHoaDon.Text + " đã thuộc tháng khóa sổ của TTPP " + DropTTPP.SelectedItem.ToString(), uscMsgBox.enmMessageType.Error);
            }
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void btChonLai_Click(object sender, EventArgs e)
    {
        try
        {
            DropTTPP.BackColor = Color.White;
            txtNgayHoaDon.BackColor = Color.White;
            txtMaBA.BackColor = Color.White;
            txtSoHoaDon.BackColor = Color.White;
            txtMaBA2.BackColor = Color.White;

            DropTTPP.Enabled = true;
            txtNgayHoaDon.Enabled = true;
            txtMaBA.ReadOnly = false;
            txtSoHoaDon.ReadOnly = false;
            txtMaBA2.ReadOnly = false;

            flag = false;
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void btChuyenDoanhSo_Click(object sender, EventArgs e)
    {
        try
        {
            dt = new ClassExecute.ClassExecute();
            if (flag)
            {
                bool kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "[ls_changeBA4Invoice]", ref error,
                    new SqlParameter("@invNo", txtSoHoaDon.Text.Trim()),
                    new SqlParameter("@invDate", txtNgayHoaDon.Text.Trim()),
                    new SqlParameter("@toSalesPKey", txtMaBA2.Text.Trim()));
                if (kq)
                {
                    uscMsgBox1.AddMessage("Chuyển doanh số thành công", uscMsgBox.enmMessageType.Success);
                    Reset();

                }
                else
                {
                    uscMsgBox1.AddMessage("Chuyển doanh số thất bại", uscMsgBox.enmMessageType.Error);
                }
                flag = false;
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "thongbao", "alert('Bạn chưa thực hiện nghiệp vụ kiểm tra')", true);
            }
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void btXoaManHinh_Click(object sender, EventArgs e)
    {
        try
        {
            Reset();
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    void Reset()
    {

        txtMaBA.Text = "";
        txtHoTenBA.Text = "";
        txtMaBA2.Text = "";
        txtHoTenBA2.Text = "";
        txtSoHoaDon.Text = "";
        txtTienHD.Text = "";
        DropTTPP.SelectedIndex = 0;
        txtNgayHoaDon.Text = DateTime.Now.ToString("dd/MM/yyyy");

        DropTTPP.BackColor = Color.White;
        txtNgayHoaDon.BackColor = Color.White;
        txtMaBA.BackColor = Color.White;
        txtSoHoaDon.BackColor = Color.White;
        txtMaBA2.BackColor = Color.White;

        DropTTPP.Enabled = true;
        txtNgayHoaDon.Enabled = true;
        txtMaBA.ReadOnly = false;
        txtSoHoaDon.ReadOnly = false;
        txtMaBA2.ReadOnly = false;
    }
    void GetTTPP()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "[ls_getDistributorList]", CommandType.StoredProcedure);
        DropTTPP.DataSource = db;
        DropTTPP.DataTextField = "sitename";
        DropTTPP.DataValueField = "Sitecode";
        DropTTPP.DataBind();
    }
    void LockText()
    {
        DropTTPP.BackColor = Color.Yellow;
        txtNgayHoaDon.BackColor = Color.Yellow;
        txtMaBA.BackColor = Color.Yellow;
        txtSoHoaDon.BackColor = Color.Yellow;
        txtMaBA2.BackColor = Color.Yellow;

        DropTTPP.Enabled = false;
        txtNgayHoaDon.Enabled = false;
        txtMaBA.ReadOnly = true;
        txtSoHoaDon.ReadOnly = true;
        txtMaBA2.ReadOnly = true;
    }
}
